package com.woniuxy.creditsystem.productmag.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.*;
import com.woniuxy.creditsystem.common.bean.ResultMsg;
import com.woniuxy.creditsystem.common.util.TraceUtil;
import com.woniuxy.creditsystem.productmag.service.IProduct;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.util.Date;
import java.util.List;

/**
 * 合作产品控制类 夏先生
 * */
@RequestMapping("/product")
@RestController
public class ProductController {

    @Resource
    private IProduct  iProductImpl;


    /**
     *
     * @param request      分页请求
     * @param productInfo  产品信息
     * @return
     */

    @GetMapping(value="/page",produces = {"application/json;charset=utf-8"})
    public TableResult<ProductInfo> findAllByParams(PageRequest request, ProductInfo productInfo){
        System.out.println(request);
        System.out.println(productInfo);


        PageInfo<ProductInfo> pageInfo = iProductImpl.findAllByParams(request,productInfo);
        return new  TableResult<ProductInfo>(pageInfo.getTotal(),pageInfo.getList());
    }


    /**
     * 新增用户
     * @param productInfo
     * @return
     */
    @PostMapping(value = "/more",produces = {"application/json;charset=utf-8"})
    public ResultMsg saveProduct(ProductInfo productInfo){
//        System.out.println("俺到后台了！");
//        System.out.println(productInfo.getProductDescp());
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        UserInfo userBean = (UserInfo) session.getAttribute("user");

        //最后更新人
        productInfo.setEndPerson(userBean.getUserName());


        //以时间戳为订单号
        String productNums = new Date().getTime()+"";
        System.out.println(productNums);

        productInfo.setProductNums(productNums);

        iProductImpl.savaProductInfo(productInfo);

        ResultMsg resultMsg = new ResultMsg(200,"新增成功",null);

        return resultMsg;
    }


    /**
     * 根据ID进行编辑修改
     * @param productInfo
     * @param id
     * @return
     */
    @PutMapping(value="/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg updaProductInfoById(ProductInfo productInfo, @PathVariable("id") Long id){
//        RoleInfo roleInfo=new RoleInfo();
//        roleInfo.setId(roleInfo_id);
//        userInfo.setRoleInfo(roleInfo);


        //拿登陆用户
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        UserInfo userBean = (UserInfo) session.getAttribute("user");

        productInfo.setEndPerson(userBean.getUserName());
        System.out.println(userBean);
        System.out.println("------------------");
        System.out.println(userBean.getUserName());


        //System.out.println("我到修改后端了");



        ResultMsg resultMsg = new ResultMsg(200,"修改成功",null);
        iProductImpl.updaProductInfo(productInfo);

        return resultMsg;
    }


    @PutMapping(value = "/upsta",produces = {"application/json;charset=utf-8"})
    public  ResultMsg updaStatusById(ProductInfo productInfo){

        System.out.println("我到更改状态的后台了");
        System.out.println(productInfo);
        ResultMsg resultMsg = new ResultMsg(200,"修改成功",null);
        iProductImpl.updaStatusById(productInfo);

        return resultMsg;
    }


    /**
     *产看界面对应渠道的产品的展示
     */
    @GetMapping(value = "/ditchproduct",produces = {"application/json;charset=utf-8"})
    public List<ProductInfo> findProductByDitchId(ProductInfo productInfo){

        System.out.println(productInfo);
        System.out.println("我到查看界面里面的数据展示层了");

        List<ProductInfo>  produByDit =  iProductImpl.findProductByDitchId(productInfo);
       return  produByDit;

    }

    /**
     * 把前台传回来的table中的UserInfo list集合 导出到一个Excel文件
     * @author hejie
     * @param tableDataJson
     * @return ResultMsg
     */
    @PostMapping(value="/exportList",produces = {"application/json;charset=utf-8"})
    public com.woniuxy.creditsystem.common.util.ResultMsg tableDataJson(@RequestBody List<ProductInfo> tableDataJson) throws IOException {
        iProductImpl.write2excel(tableDataJson,"D:/excel/output");

        //System.out.println("到导出的后台了");

        System.out.println("\n"+TraceUtil.positionTrace());
        for (ProductInfo productInfo:tableDataJson) {
            System.out.println(productInfo);
        }
        com.woniuxy.creditsystem.common.util.ResultMsg resultMsg = new com.woniuxy.creditsystem.common.util.ResultMsg(200,"in tableDataJson()",null);
        return resultMsg;
    }




}
